package de.tum.in.eist.im.client;


import java.util.Iterator;

import com.google.gwt.core.client.EntryPoint;
//<<<<<<< .mine
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
//=======
import com.google.gwt.user.client.Window;
//>>>>>>> .r9
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import de.tum.in.eist.im.client.domain.User;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class EIST_IM_thea implements EntryPoint{

	private VerticalPanel chatPanel = new VerticalPanel();
	private Label title = new Label("EIST - Instant Messaging since 2011 a.d.");
	private Label logintitle = new Label("Login");
	private Grid loginGrid = new Grid(3, 2);
	private TextBox UsernameTextBox = new TextBox();
	private PasswordTextBox PasswordTextBox = new PasswordTextBox();
	private HorizontalPanel buttonPanel = new HorizontalPanel();
	private Button RegisterButton = new Button("Register");
	private Button LoginButton = new Button("Login");
	private Label UsernameLabel = new Label("Username:");
	private Label PasswordLabel = new Label("Password:");
	

	//list of registered Users --> we will need it later!!
	public FlowPanel list = new FlowPanel();
	public FlowPanel passwordlist = new FlowPanel();

	

	public void onModuleLoad() {
		// have fun coding! :-)
		// :)

		// Login Grid!
		chatPanel.setStyleName("chatPanel");

		/*
		 * TODO: GET TITLE DESIGNED LIKE AN HEADLINE_1 IN CSS AND LOGINTITLE A
		 * LITTLE SMALLER MIDDLE- AND CENTER-ALIGN THE WHOLE LOGIN MENU E.G.
		 * CHATPANEL! FOR LOOK AND FEEL SEE THE CSS FILE ASSOCIATED WITH THE
		 * setStyleName COMMAND!
		 */
		
		chatPanel.add(title);
		title.setStyleName("title");
		chatPanel.add(logintitle);
		logintitle.setStyleName("logintitle");
		
		loginGrid.setStyleName("loginGrid");
		chatPanel.add(loginGrid);
		
		// TODO: FIX USERNAMELABEL AND PASSWORDLABELS ALIGN, BOTH SHOULD BE
		// MIDDLE ALIGNED OR EVEN LEFT
		
		loginGrid.setWidget(0, 0, UsernameLabel);
		loginGrid.setWidget(0, 1, UsernameTextBox);
		loginGrid.setWidget(1, 0, PasswordLabel);
		loginGrid.setWidget(1, 1, PasswordTextBox);
		loginGrid.setWidget(2, 1, buttonPanel);
		
		// TODO: FIX BUTTONS POSITION ONE ON THE LEFT SIDE ONE ON THE RIGHT SIDE
		
		buttonPanel.setCellHorizontalAlignment(RegisterButton,
				HasHorizontalAlignment.ALIGN_LEFT);
		buttonPanel.setCellHorizontalAlignment(LoginButton,
				HasHorizontalAlignment.ALIGN_RIGHT);
		buttonPanel.add(RegisterButton);
		buttonPanel.add(LoginButton);

		// OnClickHandler for the LoginButton
		LoginButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				// TODO:GET ONKLICK HANDLER CHECKS USERNAME AND PASSWORD AND
				// PUSHES FORWARD TO CHAT_UI

			}
		});
		
		RegisterButton.addClickHandler(new ClickHandler() {
			
			
			public void onClick(ClickEvent event) {
				
			    String registername  = UsernameTextBox.getText().toString();
			    Label nameLabel = new Label();
			    String registerpasswd = UsernameTextBox.getText().toString();
			    Label passwordLabel = new Label();
				User registerUser = new User(registername,registerpasswd);
				HorizontalPanel userPanel = new HorizontalPanel();
												
			    if(findUser(registerUser)==null){
					list.add(nameLabel);
					passwordlist.add(passwordLabel);
					nameLabel.setText(registername);
					Window.alert("User erfolgreich registriert!");
				
				}
				else if (findUser(registerUser)== registerUser){
					Window.alert("Der User kann nicht registriert werden, er existiert bereits!");
				}
			    
			    UsernameTextBox.setText("");
			    PasswordTextBox.setText("");

			}

			private User findUser(User registerUser) {				
				for(Iterator<Widget> i = list.iterator(); i.hasNext()==true;){
					
					Label a = (Label) i.next();	
					
				    if(registerUser.getUsername().equals(a.getText().toString())){
				    	return registerUser;
				    }   
				    if(i.hasNext()==false){
				    	return null;
				    }		    
				}
				return null;
				
			}});		
		
		// Associate the Main panel with the HTML host page.
		RootPanel.get("chat").add(chatPanel);
		
		//only to see the list of registered Users:
		RootPanel.get().add(list);
		RootPanel.get().add(passwordlist);

	}}
	
		
